package com.it.emall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.emall.domain.Ingredients;
import com.it.emall.service.IngredientsService;
import com.it.emall.utils.ResponseUtils;
import com.it.emall.vo.IngredientsSelectVo;
import com.it.emall.vo.IngredientsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 食品分类controller
 */
@RestController
@RequestMapping("/admin/category")
public class IngredientsController {

    @Autowired
    private IngredientsService ingredientsService;

    @GetMapping("search")
    public Object search(@RequestParam String name){
        List<Ingredients> ingredients = ingredientsService.selectIngredients(name);
        return ResponseUtils.ok(ingredients);


    }

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit){


        //分页
        PageHelper.startPage(page,limit);
        List<Ingredients> list = ingredientsService.selectIngredientsList();
        PageInfo<Ingredients> pageInfo = new PageInfo<>(list);
        //组织data数据
        Map<String,Object> data = new HashMap<>();
        //组织Vo
        List<Ingredients> level1List = pageInfo.getList();
        List<IngredientsVo> voList = new ArrayList<>();
//        查询一级分类下的二级分类
        for (Ingredients l1:level1List){
            List<Ingredients> children = ingredientsService.selectChildrenIngreByPid(l1.getId());
            IngredientsVo vo = new IngredientsVo();
            vo.setId(l1.getId());
            vo.setName(l1.getName());
            vo.setDesc(l1.getDesc());
            vo.setIconUrl(l1.getIconUrl());
            vo.setPicUrl(l1.getPicUrl());
            vo.setLevel(l1.getLevel());
            vo.setChildren(children);
            voList.add(vo);
        }
        data.put("list",voList);
        data.put("total",pageInfo.getTotal());
        data.put("pages",pageInfo.getPages());
        data.put("page",pageInfo.getPageNum());
        data.put("limit",pageInfo.getPageSize());

        return ResponseUtils.ok(data);

    }

    /**
     * 增加
     * @param ingredients
     * @return
     */

    @PostMapping("/create")
    public Object add(@RequestBody Ingredients ingredients){
        ingredients.setAddTime(new Date());
        ingredients.setUpdateTime(ingredients.getAddTime());
        ingredients.setDeleted(0);
        int i = ingredientsService.insertIngredients(ingredients);
        if(i > 0 ){
            return ResponseUtils.SUCCESS;
        }else {
            return ResponseUtils.INVALID_DEFAULT;
        }

    }

    /**
     * 修改
     * @param ingredients
     * @return
     */
    @PostMapping("/update")
    public Object edit(@RequestBody Ingredients ingredients){
        ingredients.setUpdateTime(new Date());
        int i = ingredientsService.updateIngredients(ingredients);
        if(i > 0 ){
            return ResponseUtils.SUCCESS;
        }else {
            return ResponseUtils.INVALID_DEFAULT;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */

    @GetMapping("/delete")
    public Object delete(@RequestParam Integer id){

        int i = ingredientsService.deleteIngredients(id);
        if(i > 0 ){
            return ResponseUtils.SUCCESS;
        }else {
            return ResponseUtils.INVALID_DEFAULT;
        }
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/read")
    public Object detail(Integer id){
        Ingredients ingredients = ingredientsService.selectIngredientsById(id);
        return ResponseUtils.ok(ingredients);

    }

    @GetMapping("l1")
    public Object level1List(){
        List<Ingredients> list = ingredientsService.selectIngredientsList();
        ArrayList<IngredientsSelectVo> voList = new ArrayList<>();
        for (Ingredients ingredients : list){
            IngredientsSelectVo vo = new IngredientsSelectVo();
            vo.setLabel(ingredients.getName());
            vo.setValue(ingredients.getId());
            voList.add(vo);


        }
        Map<String,Object> data = new HashMap<>();
        data.put("list",voList);
        return ResponseUtils.ok(data);

    }
}
